package com.cy.网络编程.tcp.work;

import java.io.*;
import java.net.Socket;

/**
 * 工具类，负责发送和接受消息
 */
public class SocketUtil {
    //1.发送消息
    public static void send(Socket s, String msg) {
        try {
            OutputStream os = s.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write(msg);
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //2.接受消息
    public static String rece(Socket s) {
        try {
            InputStream is = s.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            return br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
